<?php
namespace Modules\Frontend\Controllers;

class UserController extends ControllerBase {
  public function findFirstByAccountAction() {
    return $this -> returnByJson($this -> user -> getSharedFactory() -> getFacade() -> findFirstByAccount(
      $this -> request -> getQuery('account', 'string', null, true)
    ));
  }

  public function updateLoginPasswordByTokenAction() {
    return $this -> returnByJson($this -> user -> getSharedFactory() -> getFacade() -> updateLoginPasswordByToken(
      $this -> request -> getQuery('token', 'string', null, true),
      $this -> request -> getPut()
    ));
  }

  public function updateSafePasswordByTokenAction() {
    return $this -> returnByJson($this -> user -> getSharedFactory() -> getFacade() -> updateSafePasswordByToken(
      $this -> request -> getQuery('token', 'string', null, true),
      $this -> request -> getPut()
    ));
  }

  public function findFirstByTokenAction() {
    return $this -> returnByJson($this -> user -> getSharedFactory() -> getFacade() -> findFirstByToken($this -> request -> getQuery('token', 'string', null, true)));
  }

  public function loginAction() {
    $post = $this -> request -> getPost();
    if(
      !isset($post['captcha']) ||
      empty($post['captcha'])  ||
      !$this -> session -> has('frontend_captcha') ||
      ($this -> session -> get('frontend_captcha') != $post['captcha'])
    ) {
      return $this -> returnFailByJson(['msg' => '验证码错误']);
    }
    unset($post['captcha']);

    return $this -> returnByJson($this -> user -> getSharedFactory() -> getFacade() -> encryptTokenByLogin($post));
  }

  public function createAction() {
    $post = $this -> request -> getPost();
    if(!isset($post['captcha']) && empty($post['captcha'])) {
      return $this -> returnFailByJson(['msg' => '验证码错误']);
    }
    unset($post['captcha']);

    return $this -> returnByJson($this -> user -> getSharedFactory() -> getFacade() -> createHasPAccount($post));
  }
}
